<?php
namespace Api\Controller;
use Think\Controller;
class TicketController extends BaseController{
  /**
  *获取门票信息 get
  *@param: exhibitionId
  *@return:
  */
  public function getExhibitionTickets(){
    $exhibitionId = I('get.exhibitionId');
    if(!$exhibitionId){
      $this->response(array('info'=>'参数错误！'),json,400);
    }
    $data = M('ticket')
    ->field('ticket.*,exhibition.name as ex_name')
    ->where(array('status'=>1))
    ->join('exhibition on exhibition.id = ticket.exhibition_id','left')
    ->select();
    $re = $this->translate($data);
    $this->response(array('info'=>$re),json,200);
  }

   /**
  *获取门票详情 get
  *@param: ticketId
  *@return:
  */
  public function getTicketDetail(){
    $ticketId = I('get.ticketId');
    if(!$ticketId){
      $this->response(array('info'=>'参数错误！'),json,400);
    }
    $data = M('ticket')->find($ticketId);
    $re = $this->translate($data);
    $this->response(array('info'=>$re),json,200);
  }

   /**
  *获取兑换码详情 get
  *@param: redeem_code
  *@return:
  */
  public function getCodeDetail(){
    $redeem_code = I('get.redeem_code');
    if(!$redeem_code){
      $this->response(array('info'=>'参数错误！'),json,400);
    }
    // $data = 'abc';
    $data = M('ticket_redeem_code')
    ->field('ticket_redeem_code.*,ticket.name as ticket_name,exhibition.name as exhibition_name,ticket_order.order_num,ticket_order.create_time as order_create_time,visitor.name as user_name')
    ->where(array('redeem_code'=>$redeem_code))
    ->join('ticket_order on ticket_order.id = ticket_order_id','left')
    ->join('ticket on ticket.id = ticket_order.ticket_id','left')
    ->join('exhibition on exhibition.id = ticket.exhibition_id','left')
    ->join('visitor on visitor.id = ticket_order.visitor_id','left')
    ->find();
    $this->response(array('info'=>$data),json,200);
  }

  /**
  *根据日期查看兑换记录，分页  get
  *@param: date
  *@return:
  */
  public function getRedeemRecord(){
    $date = I('get.date');
    if(!$date){
      $this->response(array('info'=>'请输入兑换日期'),json,400);
    }
    $page = I('get.page')?I('get.page'):1;

    $data = M('ticket_redeem_code')
    ->field('ticket_redeem_code.*,ticket.name as ticket_name,exhibition.name as exhibition_name')
    ->join('ticket_order on ticket_order.id = ticket_redeem_code.ticket_order_id','left')
    ->join('ticket on ticket.id = ticket_order.ticket_id','left')
    ->join('exhibition on exhibition.id = ticket.exhibition_id','left')
    ->where("redeem_time > $date and redeem_time < ($date+3600*24)")
    ->order('ticket_redeem_code.redeem_time desc')
    ->page($page,10)
    ->select();

    $this->response(array('info'=>$data),json,200);
  }

  /**
  *根据日期查看总兑换数 get
  *@param: date
  *@return:
  */
  public function getTotalRedeem(){
    $date = I('get.date');
    if(!$date){
      $this->response(array('info'=>'请输入兑换日期'),json,400);
    }

    $amount = M('ticket_redeem_code')
    ->field('ticket_redeem_code.*,ticket.name as ticket_name,exhibition.name as exhibition_name')
    ->join('ticket_order on ticket_order.id = ticket_redeem_code.ticket_order_id','left')
    ->join('ticket on ticket.id = ticket_order.ticket_id','left')
    ->join('exhibition on exhibition.id = ticket.exhibition_id','left')
    ->where(array('ticket_redeem_code.status'=>1))
    ->where("redeem_time > $date and redeem_time < ($date+3600*24)")
    ->count();
    $data['redeem_amount'] = $amount;
    $data['signin_amount'] = 0;
    $this->response(array('info'=>$data),json,200);
  }

  /**
  *下单 post
  *@param: ticket_id,amount
  *@return:
  */
  public function confirmTicketOrder(){
    $uid = $this->securityTest();
    // $post = json_decode(file_get_contents('php://input'), true);
    $post = I('post.');
    if(!$post['ticket_id']){
      $this->response(array('info'=>'参数错误'),json,400);
    }
    if(!$post['amount']||$post['amount']<=0){
      $this->response(array('info'=>'请选择至少一张票进行购买'),json,400);
    }

    //查询用户信息
    $re_visitor = M('visitor')->find($uid);
    $post['create_time'] = time();
    $post['visitor_id'] = $uid;
    $re_order = M('ticket_order')->add($post);
    M('ticket_order')->where(array('id'=>$re_order))->save(array('order_num'=>date("Ymd").$re_order));

    for($i=0;$i<$post['amount'];$i++){
      $data['ticket_order_id'] = $re_order;
      $data['create_time'] = time();
      $data['visitor_phone'] = $re_visitor['phone'];
      $re_redeem = M('ticket_redeem_code')->add($data);
      $len = strlen($re_redeem);
      $id = substr($re_redeem,($len-2));
      M('ticket_redeem_code')->where(array('id'=>$re_redeem))->save(array('redeem_code'=>$data['create_time'].$id));
    }
    
    $this->response(array('info'=>$re_order),json,200);
  }

  /**
  *兑换 post
  *@param: redeem_code
  *@return:
  */
  public function exchangeTicket(){
    // $uid = $this->securityTest();
    $post = json_decode(file_get_contents('php://input'), true);
    // $post = I('post.');
    if(!$post['redeem_code']){
      $this->response(array('info'=>'未获取兑换码信息！'),json,400);
    }
    $re_redeem = M('ticket_redeem_code')->where(array('redeem_code'=>$post['redeem_code']))->find();
    if($re_redeem['status']==='1'){
      $this->response(array('info'=>'该票券已兑换！'),json,400);
    }
    $re = M('ticket_redeem_code')->where(array('redeem_code'=>$post['redeem_code']))->save(array('status'=>1,'redeem_time'=>time()));

    $this->response(array('info'=>'success'),json,200);
  }

  /**
  *获取订单兑换码
  *@param: ticketOrderId
  *@return:
  */
  public function getRedeemCodes(){
    $uid = $this->securityTest();
    $ticketOrderId = I('get.ticketOrderId');
    if(!$ticketOrderId){
      $this->response(array('info'=>'参数错误'),json,400);
    }
    $data = M('ticket_redeem_code')
    ->field('ticket_redeem_code.*,ticket_order.order_num')
    ->where(array('ticket_order_id'=>$ticketOrderId))
    ->join('ticket_order on ticket_order.id = ticket_redeem_code.ticket_order_id','left')
    ->order('create_time desc')
    ->select();

    $this->response(array('info'=>$data),json,200);
  }

  /**
  *获取所有兑换码
  *@param: 
  *@return:
  */
  public function getAllRedeemCodes(){
    $uid = $this->securityTest();
    $data = M('ticket_redeem_code')
    ->field('ticket_redeem_code.*,ticket_order.order_num,ticket_order.visitor_id')
    ->where(array('ticket_order.visitor_id'=>$uid))
    ->join('ticket_order on ticket_order.id = ticket_redeem_code.ticket_order_id','left')
    ->order('create_time desc')
    ->select();

    $this->response(array('info'=>$data),json,200);
  }

  /**
  *取消免费票 post
  *@param: 
  *@return:
  */
  public function cancelTicketOrder(){
    $uid = $this->securityTest();
    $post = json_decode(file_get_contents('php://input'), true);
    if(!$post['order_id']){
      $this->response(array('info'=>'未获取门票信息！'),json,400);
    }
    $re_order = M('ticket_order')->find($post['order_id']);
    $re_ticket = M('ticket')->where(array('id'=>$re_order['ticket_id']))->find();
    if($re_order['price']>0){
      $this->response(array('info'=>'抱歉，您的门票不可取消，请联系客服！'),json,400);
    }
    M('ticket_order')->where(array('id'=>$post['order_id']))->save(array('status'=>4));
    M('ticket_redeem_code')->where(array('ticket_order_id'=>$post['order_id']))->save(array('status'=>1));
    $new_stock = $re_ticket['stock'] + $re_order['amount'];
    M('ticket')->where(array('id'=>$re_order['ticket_id']))->save(array('stock'=>$new_stock));

    $this->response(array('info'=>'success'),json,200);
  }

  // 闸机核销
  public function exchangeRedeemcodeByNumber(){
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST)){
     $_POST = (array)json_decode(file_get_contents('php://input'), true);
    }
    $post = I('post.');
    $redeem_id = $post['redeem_num'];
    $time = $post['timestamp'];
    $sign = $post['sign'];
    if(!$redeem_id||!$time){
      $this->response(array('status'=>400,'info'=>'缺少参数'),json,400);
    }
    if($sign!=md5($post['redeem_num'].$post['timestamp'])){
      $this->response(array('status'=>400,'info'=>'校验失败'),json,400);
    }
    //检查是否是兑换码
    $re_redeem = M('ticket_redeem_code')->where(array('redeem_code'=>$redeem_id))->find();
    if(!$re_redeem){
      $this->response(array('status'=>400,'info'=>'未找到兑换码信息，请重新扫码'),json,400);
    }
    if($re_redeem['status'] == '1'){
      $this->response(array('status'=>400,'info'=>'此码已兑换过，请扫描新的兑换码'),json,400);
    }
    M('ticket_redeem_code')->where(array('redeem_code'=>$re_redeem['redeem_code']))->save(array('status'=>1,'redeem_time'=>time()));
    $re_redeem = M('ticket_redeem_code')
    ->field('ticket_redeem_code.*,exhibition.name as exhibition_name,ticket.name as ticket_name')
    ->where(array('redeem_code'=>$re_redeem['redeem_code']))
    ->join('ticket_order on ticket_order.id = ticket_redeem_code.ticket_order_id','left')
    ->join('ticket on ticket.id = ticket_order.ticket_id','left')
    ->join('exhibition on exhibition.id = ticket.exhibition_id','left')
    ->find();
    $this->response(array('status'=>200,'info'=>'兑换成功'),json,200);
  }

  // 闸机核销恢复
  public function reset(){
    M('ticket_redeem_code')->where(array('id'=>'141172'))->save(array('status'=>0));
    $this->response(array('status'=>200,'info'=>'重置成功'),json,200);
  }

}